<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class GoodsController extends BaseController {

//=====================================曲谱操作=======================================
    //曲谱列表
    public function index_qp(){
        $data = array_filter($_GET);
        if (!empty($_GET['goods_num'])) {
            $data['goods_num'] = array('like',"%".trim($_GET['goods_num'])."%");
        }
        unset($data['p']);
        $data['goods_category'] = 1;
        $data['goods_display'] = 1;
        $count = M('goods')->where($data)->count();
        $page = getpage($count,10);
        $goods_arr = M('goods')->where($data)->order('goods_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('goods_arr', $goods_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //添加曲谱
    public function qp_add(){
        if (I('post.action') == 'qp_add_action') {
            $this->qp_add_action();
            exit();
        }
        $this->display();
    }

    //添加曲谱处理
    public function qp_add_action(){

        if($_POST != array_filter($_POST)){
            $this->error('曲谱选项不能有空值！');
        }
        //文件上传
        $finfopic = upload($_FILES['goods_pic']);
        //曲谱封面图上传
        if ($finfopic['status'] == 0) {
            if ($finfopic['msg'] == 'nofile') {
                $this->error('曲谱封面图未上传');
            }
            $this->error($finfopic['msg']);
        } elseif ($finfopic['status'] == 1) {
            $_POST['goods_pic'] = $finfopic['filepath'];
        }

        $_POST['goods_time'] = date('Y-m-d H:i:s',time());
        $_POST['goods_category'] = 1;
        if(M('goods')->add($_POST)){
            $this->success('添加曲谱成功','index_qp');
        }else{
            $this->error('添加曲谱失败');
        }

    }

    //曲谱详情
    public function qp_details(){
        $data['goods_id'] = I('get.goods_id');
        $goods_arr = M('goods')->where($data)->find();
        $this->assign('goods_arr',$goods_arr);
        $this->display();
    }

    //删除曲谱
    public function qp_del(){
        $data['goods_id'] = I('get.goods_id');
        $data['goods_display'] = 0;
        M('goods')->save($data);
        $this->redirect('index_qp');
    }

    //修改曲谱
    public function qp_edit(){
        if (I('post.action') == 'qp_edit_action') {
            $this->qp_edit_action();
            exit();
        }
        $data['goods_id'] = I('get.goods_id');
        $goods_arr = M('goods')->where($data)->find();
        $this->assign('goods_arr',$goods_arr);
        $this->display();
    }

    //修改曲谱操作
    public function qp_edit_action(){
        if($_POST != array_filter($_POST)){
            $this->error('曲谱选项不能有空值！');
        }
        //文件上传
        $finfopic = upload($_FILES['goods_pic']);
        //曲谱封面图上传
        if ($finfopic['status'] == 0 && $finfopic['msg'] != 'nofile') {
            $this->error($finfopic['msg']);
        } elseif ($finfopic['status'] == 1) {
            $_POST['goods_pic'] = $finfopic['filepath'];
        }

        if(M('goods')->save($_POST)){
            $this->success('修改曲谱成功','index_qp');
        }else{
            $this->error('修改曲谱失败');
        }
    }

//=====================================教程操作=======================================

    //教程列表
    public function index_jc(){
        $data = array_filter($_GET);
        if (!empty($_GET['goods_num'])) {
            $data['goods_num'] = array('like',"%".trim($_GET['goods_num'])."%");
        }
        unset($data['p']);
        $data['goods_category'] = 2;
        $data['goods_display'] = 1;
        $count = M('goods')->where($data)->count();
        $page = getpage($count,10);
        $goods_arr = M('goods')->where($data)->order('goods_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('goods_arr', $goods_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //添加教程
    public function jc_add(){
        if (I('post.action') == 'jc_add_action') {
            $this->jc_add_action();
            exit();
        }
        $this->display();
    }

    //添加教程处理
    public function jc_add_action(){
        if($_POST != array_filter($_POST)){
            $this->error('教程选项不能有空值！');
        }
        //文件上传
        $finfopic = upload($_FILES['goods_pic']);
        //教程封面图上传
        if ($finfopic['status'] == 0) {
            if ($finfopic['msg'] == 'nofile') {
                $this->error('教程封面图未上传');
            }
            $this->error($finfopic['msg']);
        } elseif ($finfopic['status'] == 1) {
            $_POST['goods_pic'] = $finfopic['filepath'];
        }

        $_POST['goods_time'] = date('Y-m-d H:i:s',time());
        $_POST['goods_category'] = 2;
        if(M('goods')->add($_POST)){
            $this->success('添加教程成功','index_jc');
        }else{
            $this->error('添加教程失败');
        }

    }

    //教程详情
    public function jc_details(){
        $data['goods_id'] = I('get.goods_id');
        $goods_arr = M('goods')->where($data)->find();
        $this->assign('goods_arr',$goods_arr);
        $this->display();
    }

    //删除教程
    public function jc_del(){
        $data['goods_id'] = I('get.goods_id');
        $data['goods_display'] = 0;
        M('goods')->save($data);
        $this->redirect('index_jc');
    }

    //修改教程
    public function jc_edit(){
        if (I('post.action') == 'jc_edit_action') {
            $this->jc_edit_action();
            exit();
        }
        $data['goods_id'] = I('get.goods_id');
        $goods_arr = M('goods')->where($data)->find();
        $this->assign('goods_arr',$goods_arr);
        $this->display();
    }

    //修改教程操作
    public function jc_edit_action(){
        if($_POST != array_filter($_POST)){
            $this->error('教程选项不能有空值！');
        }
        //文件上传
        $finfopic = upload($_FILES['goods_pic']);
        //教程封面图上传
        if ($finfopic['status'] == 0 && $finfopic['msg'] != 'nofile') {
            $this->error($finfopic['msg']);
        } elseif ($finfopic['status'] == 1) {
            $_POST['goods_pic'] = $finfopic['filepath'];
        }
        if(M('goods')->save($_POST)){
            $this->success('修改教程成功','index_jc');
        }else{
            $this->error('修改教程失败');
        }
    }

}